package timing.ukulele.gateway.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 负载均衡配置
 */
@Configuration
public class LoadBalancerConfig {

    /**
     * 缓存管理，使用Caffeine
     * @return CaffeineCacheManager对象
     */
    @Bean
    public CaffeineCacheManager caffeineCacheManager() {
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(10, TimeUnit.MINUTES).recordStats();
        CaffeineCacheManager cache = new CaffeineCacheManager("loadBalancedCache");
        cache.setCaffeine(caffeine);
        return cache;
    }
}
